package data.dictionary;

import java.util.Map;
import java.util.StringTokenizer;

import data.mapper.Mapper;
import data.preprocessing.EnglishPorterStemmer;
import data.preprocessing.FrenchPorterStemmer;
import data.preprocessing.StringProcessor;
import data.preprocessing.StringProcessor_FromStringProcessors;
import data.preprocessing.StringProcessor_LowerCase;
import data.preprocessing.StringProcessor_RemoveLineAndSpace;
import data.preprocessing.StringProcessor_RemovePunctuation;
import data.preprocessing.StringProcessor_RemoveShortWords;
import data.vectors.DoubleVector;
import data.vectors.DoubleVectorWritable;
import data.vectors.implementation.DoubleVector_Sparse_Memory;
import tools.*;

public class Dictionary_Mapper implements Mapper<String, DoubleVector>{

	
	private Dictionary_Memory<String> dico;
	private StringProcessor sp;
	
	
	
	public Dictionary_Mapper(Dictionary_Memory<String> dico,
			StringProcessor sp) {
		super();
		this.dico = dico;
		this.sp = sp;
	}


	public static StringProcessor standardStringProcessor() {

		StringProcessor_FromStringProcessors sp = new StringProcessor_FromStringProcessors() ;
		sp.add(new EnglishPorterStemmer());
		sp.add(new StringProcessor_RemoveShortWords(2));
		sp.add(new StringProcessor_RemoveLineAndSpace());
		sp.add(new StringProcessor_LowerCase());
		sp.add(new StringProcessor_RemovePunctuation());
		sp.add(new StringProcessor_RemoveLineAndSpace()) ;

		return sp;
	}
	
	/**
	 * Version du standardStringProcessor AVEC hashmap mot->stem
	 * @return
	 */
	public static StringProcessor standardStringProcessor(Map<String,String> m) {

		StringProcessor_FromStringProcessors sp = new StringProcessor_FromStringProcessors() ;
		sp.add(new EnglishPorterStemmer(m));
		sp.add(new StringProcessor_RemoveShortWords(2));
		sp.add(new StringProcessor_RemoveLineAndSpace());
		sp.add(new StringProcessor_LowerCase());
		sp.add(new StringProcessor_RemovePunctuation());
		sp.add(new StringProcessor_RemoveLineAndSpace()) ;

		return sp;
	}

	public DoubleVectorWritable map(String from) {
		
		
		StringTokenizer st = new StringTokenizer(sp.map(from));
		
		DoubleVectorWritable ret = new DoubleVector_Sparse_Memory();
		while(st.hasMoreTokens()){
			int index = dico.indexOf(st.nextToken());
			
			ret.setValue(index, ret.getValue(index)+1);
		}
		return ret;
		
	}
	
}
